package com.example.neusoft215gdh.androidpro_21.Adapter;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.neusoft215gdh.androidpro_21.Activity.PeiXunDetailActivity;
import com.example.neusoft215gdh.androidpro_21.JavaBean.PeiXunJiBie;
import com.example.neusoft215gdh.androidpro_21.R;
import com.squareup.picasso.Picasso;

import java.util.List;
/**培训列表适配器
 * Created by hp-pc on 2017/9/28.
 */
public class peiXunAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

private Context mContext;//承载上下文
private List<PeiXunJiBie> mDataList;//动态数组承载数据使用
private LayoutInflater mLayoutInflater;

    public peiXunAdapter(Context mContext,List<PeiXunJiBie> mDataList){
        this.mContext=mContext;
        this.mDataList=mDataList;
        mLayoutInflater=LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=mLayoutInflater.from(parent.getContext()).inflate(R.layout.train,parent,false);
        return new ViewHolder(v);
    }

    //将数据绑定到控件
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
     final PeiXunJiBie entity=mDataList.get(position);
        if (null==entity)
            return;
        ViewHolder viewHolder= (ViewHolder) holder;
        viewHolder.name.setText(entity.getName());
        if(!entity.getThumb().isEmpty())
        {
            Picasso.with(mContext).load("http://172.24.10.175/workout/Uploads/"+entity.getThumb()).into(viewHolder.img);
        }
        else
        {
            viewHolder.img.setImageResource(R.drawable.ic_person);
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(mContext, PeiXunDetailActivity.class);
                        intent.putExtra("id",entity.getId());
                        mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    //找到控件，将其初始化
    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView img;
        TextView name;
        public ViewHolder(View itemView) {
            super(itemView);
            img= (ImageView) itemView.findViewById(R.id.tra_img);
            name= (TextView) itemView.findViewById(R.id.tra_name);

        }
    }
}
